<template>
  <div class="my-tag">
    <input
      class="input"
      ref="inp"
      type="text"
      placeholder="输入标签"
      v-if="flag"
      :value="value"
      @blur="close"
      @keyup.enter="$event.target.blur()"
    />
    <div class="text" v-else @dblclick="open">{{ value }}</div>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: String,
      require: true,
    },
  },
  data() {
    return {
      flag: false,
    };
  },
  methods: {
    open() {
      this.flag = true;
      this.$nextTick(() => {
        this.$refs.inp.focus();
      });
    },
    close(e) {
      if (!e.target.value) return;
      this.flag = false;
      this.$emit("input", e.target.value);
    },
  },
};
</script>

<style lang="less">
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>
